/*
 * To change this template, choose Utils | Templates
 * and open the template in the editor.
 */
package avenue;

import customers.ListClientForm;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import model.DBManager;
import program.ListProgramForm;
import reports.CollectionsReportForm;
import reports.PaymentReportForm;
import roles.ListRolesForm;
import tolls.CollectTollForm;
import users.ListUsersForm;

/**
 *
 * @author Aggelos
 */
public class Avenue extends javax.swing.JFrame {

    /**
     * Creates new form Avenue
     */
    //private JFrame thisFrame;
    private EntityManager em;
    private JFrame thisFrame;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

    public Avenue() {
        this.setTitle(Utils.appTitle());
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //InputContext ic = InputContext.getInstance(); 
        initComponents();

        log("ΕΝΑΡΞΗ ");
        log("Χρήστης: " + Session.loginUser.getUserName());
        log("Ρόλος: " + Session.loginUser.getRoleId().getRoleName());
        log("");

        this.getContentPane().setBackground(Color.LIGHT_GRAY);

        if (Session.loginUser.getSystemProtected() != 1) {
            jTextField1.setText(Session.loginUser.getStationId().getDescription());
        } else {
            jTextField1.setText("GLOBAL");
        }
        jTextField2.setText(Session.loginUser.getUserName() + ":" + Session.loginUser.getRoleId().getRoleName());

        /*this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);*/
        getjMenuUsers().setEnabled(Session.userHasRolePermission(Session.USERS_MODULE, Session.CAN_RETRIEVE));

        getjMenuCustomers().setEnabled(Session.userHasRolePermission(Session.CUSTOMERS_MODULE, Session.CAN_RETRIEVE));
        getjMenuRoles().setEnabled(Session.userHasRolePermission(Session.USERS_MODULE, Session.CAN_RETRIEVE));
        getjMenuPrograms().setEnabled(Session.userHasRolePermission(Session.PROGRAMS_MODULE, Session.CAN_RETRIEVE));
        getjMenuCollections().setEnabled(Session.userHasRolePermission(Session.TOLL_COLLECTION_MODULE, Session.CAN_RETRIEVE));
        getjMenuPayments().setEnabled(Session.userHasRolePermission(Session.PAYMENTS_MODULE, Session.CAN_RETRIEVE));
        jMenu4.setEnabled(Session.userHasRolePermission(Session.LOG_MODULE, Session.CAN_RETRIEVE));
    }

    public void log(String s) {
        if (s.trim().equals("")) {
            logBox.append("\n");
        } else {
            logBox.append("\n" + sdf.format(new Date()) + "  " + s);
        }
        logBox.setCaretPosition(logBox.getDocument().getLength());

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        logBox = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTxtTeam = new javax.swing.JTextPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuUsers = new javax.swing.JMenuItem();
        jMenuCustomers = new javax.swing.JMenuItem();
        jMenuRoles = new javax.swing.JMenuItem();
        jMenuPrograms = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuCollections = new javax.swing.JMenuItem();
        jMenuPayments = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuCollectionsReport = new javax.swing.JMenuItem();
        jMenuPaymentsReport = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuLogout = new javax.swing.JMenuItem();
        jMenuExit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Σταθμός: ");

        jTextField1.setEditable(false);
        jTextField1.setText("jTextField1");

        jLabel2.setText("Χρήστης: ");

        jTextField2.setEditable(false);
        jTextField2.setText("jTextField2");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        logBox.setEditable(false);
        logBox.setColumns(20);
        logBox.setRows(5);
        jScrollPane2.setViewportView(logBox);

        jTxtTeam.setEditable(false);
        jTxtTeam.setBackground(new java.awt.Color(227, 227, 227));
        jTxtTeam.setBorder(null);
        jTxtTeam.setText("           \n                ΑΘΗ-3\n\n     ΠΑΣΙΑΣ ΑΠΟΣΤΟΛΟΣ\n    ΠΟΛΥΔΩΡΑΣ ΓΙΑΝΝΗΣ\n  ΤΡΙΑΝΤΟΠΟΥΛΟΥ ΜΑΡΘΑ  \n    ΧΑΝΤΖΑΡΑΣ ΒΑΣΙΛΗΣ");
        jScrollPane1.setViewportView(jTxtTeam);

        jMenu1.setText("Διαχείριση");

        jMenuUsers.setText("Χρήστες");
        jMenuUsers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuUsersActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuUsers);

        jMenuCustomers.setText("Πελάτες");
        jMenuCustomers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuCustomersActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuCustomers);

        jMenuRoles.setText("Ρόλοι");
        jMenuRoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuRolesActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuRoles);

        jMenuPrograms.setText("Προγράμματα");
        jMenuPrograms.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuProgramsActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuPrograms);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Διόδια");

        jMenuCollections.setText("Διέλευση");
        jMenuCollections.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuCollectionsActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuCollections);

        jMenuPayments.setText("Πληρωμές");
        jMenuPayments.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPaymentsActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuPayments);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Κινήσεις");

        jMenuCollectionsReport.setText("Κίνηση Κάρτας");
        jMenuCollectionsReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuCollectionsReportActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuCollectionsReport);

        jMenuPaymentsReport.setText("Κίνηση Πελάτη");
        jMenuPaymentsReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPaymentsReportActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuPaymentsReport);

        jMenuBar1.add(jMenu4);

        jMenu2.setLabel("Έξοδος");

        jMenuLogout.setLabel("Αποσύνδεση");
        jMenuLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuLogoutActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuLogout);

        jMenuExit.setText("Τερματισμός");
        jMenuExit.setToolTipText("");
        jMenuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuExitActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuExit);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 733, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(232, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuPaymentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPaymentsActionPerformed
        Utils.showMessage(null, Utils.msgPaymentWay, Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);

        //showModalFrame(new PaymentForm());
    }//GEN-LAST:event_jMenuPaymentsActionPerformed

    private void jMenuCustomersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuCustomersActionPerformed
        showModalFrame(new ListClientForm());
    }//GEN-LAST:event_jMenuCustomersActionPerformed

  
    private void showModalFrame(JFrame f) {
        f.setVisible(true);
        thisFrame = this;
      
        thisFrame.setEnabled(false);
        f.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
                if (MyWindowEvent.isExitAndSave(arg0)) {
                }
                thisFrame.setEnabled(true);
            }

            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }

            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
            }

            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");

            }

            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }

            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }

            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }

    private void jMenuUsersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuUsersActionPerformed
        showModalFrame(new ListUsersForm());
    }//GEN-LAST:event_jMenuUsersActionPerformed

    private void jMenuRolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuRolesActionPerformed
        showModalFrame(new ListRolesForm());
    }//GEN-LAST:event_jMenuRolesActionPerformed

    private void jMenuCollectionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuCollectionsActionPerformed
        showModalFrame(new CollectTollForm());
    }//GEN-LAST:event_jMenuCollectionsActionPerformed

    private void jMenuProgramsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuProgramsActionPerformed
        showModalFrame(new ListProgramForm());
    }//GEN-LAST:event_jMenuProgramsActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        //Ο ΧΡΗΣΤΗΣ ΒΓΑΙΝΕΙ offline
        em = DBManager.em;
        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
        }
        Session.loginUser.setIsOnline(0);
        em.merge(Session.loginUser);
        em.getTransaction().commit();
    }//GEN-LAST:event_formWindowClosing

    private void jMenuCollectionsReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuCollectionsReportActionPerformed
        showModalFrame(new CollectionsReportForm());        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuCollectionsReportActionPerformed

    private void jMenuPaymentsReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPaymentsReportActionPerformed
        showModalFrame(new PaymentReportForm());
    }//GEN-LAST:event_jMenuPaymentsReportActionPerformed

    private void jMenuLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuLogoutActionPerformed
        // TODO add your handling code here:
        //System.exit(0);
         Utils.mainForm.invalidate();
         Utils.mainForm.setVisible(false);
         LoginForm nlg = new LoginForm();
         nlg.setVisible(true);
    }//GEN-LAST:event_jMenuLogoutActionPerformed

    private void jMenuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jMenuExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Avenue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Avenue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Avenue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Avenue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Avenue().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuCollections;
    private javax.swing.JMenuItem jMenuCollectionsReport;
    private javax.swing.JMenuItem jMenuCustomers;
    private javax.swing.JMenuItem jMenuExit;
    private javax.swing.JMenuItem jMenuLogout;
    private javax.swing.JMenuItem jMenuPayments;
    private javax.swing.JMenuItem jMenuPaymentsReport;
    private javax.swing.JMenuItem jMenuPrograms;
    private javax.swing.JMenuItem jMenuRoles;
    private javax.swing.JMenuItem jMenuUsers;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextPane jTxtTeam;
    private javax.swing.JTextArea logBox;
    // End of variables declaration//GEN-END:variables

    public JMenuItem getjMenuCustomers() {
        return jMenuCustomers;
    }

    public void setjMenuCustomers(JMenuItem jMenuItem1) {
        this.jMenuCustomers = jMenuItem1;
    }

    public JMenuItem getjMenuCollections() {
        return jMenuCollections;
    }

    public void setjMenuCollections(JMenuItem jMenuItem2) {
        this.jMenuCollections = jMenuItem2;
    }

    public JMenuItem getjMenuUsers() {
        return jMenuUsers;
    }

    public void setjMenuUsers(JMenuItem jMenuItem3) {
        this.jMenuUsers = jMenuItem3;
    }

    public JMenuItem getjMenuPayments() {
        return jMenuPayments;
    }

    public void setjMenuPayments(JMenuItem jMenuItem4) {
        this.jMenuPayments = jMenuItem4;
    }

    public JMenuItem getjMenuCollectionsReport() {
        return jMenuCollectionsReport;
    }

    public void setjMenuCollectionsReport(JMenuItem jMenuItem5) {
        this.jMenuCollectionsReport = jMenuItem5;
    }

    public JMenuItem getjMenuPaymentsReport() {
        return jMenuPaymentsReport;
    }

    public void setjMenuPaymentsReport(JMenuItem jMenuItem6) {
        this.jMenuPaymentsReport = jMenuItem6;
    }

    public JMenuItem getjMenuRoles() {
        return jMenuRoles;
    }

    public void setjMenuRoles(JMenuItem jMenuItem7) {
        this.jMenuRoles = jMenuItem7;
    }

    public JMenuItem getjMenuPrograms() {
        return jMenuPrograms;
    }

    public void setjMenuPrograms(JMenuItem jMenuItem8) {
        this.jMenuPrograms = jMenuItem8;
    }
}
